package org.example.controller;

import org.example.domain.Course;
import org.example.domain.JsonData;
import org.example.domain.Meta;
import org.example.domain.PageBean;
import org.example.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Jimmy
 * @version 1.0
 */
@Controller
@ResponseBody
public class CourseController {

    @Autowired
    private CourseService courseService;

    /**
     * 分页查询
     * @param courseId 学科id
     * @param courseName 学科名
     * @param courseRemark 学科简介
     * @param pagenum 当前页码
     * @param pagesize 每页条目数
     * @return jsonData
     */
    @GetMapping(value = "/courses")
    public JsonData<PageBean<Course>> pageBean(@RequestParam(required = false) String courseId, @RequestParam(required = false) String courseName, @RequestParam(required = false) String courseRemark, int pagenum, int pagesize){
        Meta meta;
        PageBean<Course> pageBean = courseService.findByPage(new Course(courseId, courseName, courseRemark), pagenum, pagesize);
        if (pageBean != null){
            meta = new Meta("获取成功",200);
        }else{
            meta = new Meta("获取失败",404);
        }

        return new JsonData<>(pageBean,meta);
    }

}
